package localTests;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import localTests.TFTPException;






public class TestGet {

	static DefaultHttpClient client = new DefaultHttpClient();
	static BasicHttpContext httpContext = new BasicHttpContext();
	static CookieStore cookieStore = new BasicCookieStore();
	
	public static void main(String args[])
	{
		JSONObject parameters = new JSONObject();
		JSONObject response = new JSONObject();
		try{
			
			parameters.put("numberOfThoughts", "25");
			parameters.put("stallName", "Global");
			parameters.put("stallPassword", "");
			
			//Call the web service
			response = new JSONObject(callService("GetThoughts.php",parameters));
			
			//If the value of the success key from the response object is true then populate the list
			if(response.get("success").equals("false")){
				throw new TFTPException(response.get("error").toString());
			}
		}
		catch(Exception e){
			System.out.println(e.getMessage());
		}
		
	}
	private static String callService(String service, JSONObject parameters){
		String serviceResponse;
		try{
			
			HttpPost post = new HttpPost("http://thoughtsfromthepot.com/" + service);
			post.setEntity(new StringEntity(parameters.toString()));
			httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

			System.out.println("Post");
			
			/*Header[] headers = post.getAllHeaders();
			int headerCounter = 0;
			while(headerCounter<headers.length){
				System.out.println(headers[headerCounter]);
				headerCounter++;
			}*/
			HttpResponse response = client.execute(post);
			HttpEntity entity = response.getEntity();
			InputStream is = entity.getContent();
			BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
			serviceResponse  = reader.readLine();
			System.out.println(serviceResponse);
			/*while(serviceResponse!=null){
				serviceResponse = reader.readLine();
				System.out.println(serviceResponse);
			}*/
			/*headers = response.getAllHeaders();
			headerCounter = 0;
			while(headerCounter<headers.length){
				System.out.println(headers[headerCounter]);
				headerCounter++;
			}*/
		}
		catch(Exception e){
			serviceResponse = e.getMessage();
		}
		return serviceResponse;
	}
	

}
